<?php
include_once('config/config.php');

$admin = LoginCheck();

// 当前页
$current = $_GET['page'] ?? 1;

$keyword = isset($_GET['keyword']) ? trim($_GET['keyword']) : '';
$mobile = isset($_GET['mobile']) ? trim($_GET['mobile']) : '';

// 条件变量
$where = 1;

if (!empty($keyword)) {
  $where = "`p`.`name` LIKE '%$keyword%' OR `d`.`name` LIKE '%$keyword%'";
}

if (!empty($mobile)) {
  $where .= " AND `p`.`mobile` LIKE '%$mobile%'";
}

// 设置每页显示10条数据
$limit = 3;

// 偏移量  (1 - 1) * 10 (2 - 1) * 10
$start = ($current - 1) * $limit;

// 查询当前数据表的总数
$count = find("SELECT COUNT(*) AS count FROM `pre_person` AS `p` LEFT JOIN `pre_department` AS `d` ON `p`.`depid` = `d`.`id` LEFT JOIN `pre_job` AS `j` ON `p`.`jobid` = `j`.`id` WHERE $where");

// 查询所有员工数据
$PersonData = all("SELECT `p`.*,`d`.`name` AS `dname`,`j`.`name` AS `jname` FROM `pre_person` AS `p` LEFT JOIN `pre_department` AS `d` ON `p`.`depid` = `d`.`id` LEFT JOIN `pre_job` AS `j` ON `p`.`jobid` = `j`.`id` WHERE $where ORDER BY `createtime` DESC LIMIT $start,$limit");

// 调用分页函数
$ShowPage = ShowPage($current, $count['count'], $limit);

?>

<!DOCTYPE html>
<html lang="en">

<head>
  <!-- 引入公共样式以及js -->
  <?php include_once('common/meta.php'); ?>
</head>

<body>

  <!-- 引入头部 -->
  <?php include_once('common/header.php'); ?>

  <!-- 引入菜单 -->
  <?php include_once('common/menu.php'); ?>

  <div class="content">
    <div class="header">
      <h1 class="page-title">员工列表</h1>
    </div>
    <ul class="breadcrumb">
      <li><a href="index.php">Home</a> <span class="divider">/</span></li>
      <li class="active">PersonList</li>
    </ul>

    <div class="container-fluid">
      <div class="row-fluid">
        <div class="btn-toolbar">
          <button class="btn btn-primary" onClick="location='PersonAdd.php'"><i class="icon-plus"></i>添加员工</button>

          <form method="get">
            <input type="text" name="keyword" placeholder="<?php echo $keyword ? $keyword : '请输入搜索关键字'; ?>">
            <input type="text" name="mobile" placeholder="<?php echo $mobile ? $mobile : '请输入搜索手机号'; ?>">
            <button class="btn btn-primary">搜索</button>
          </form>
        </div>
        <div class="well">
          <table class="table">
            <thead>
              <tr>
                <th>ID</th>
                <th>头像</th>
                <th>姓名</th>
                <th>手机号</th>
                <th>邮箱</th>
                <th>性别</th>
                <th>所属部门</th>
                <th>职位</th>
                <th>入职时间</th>
                <th style="width: 26px;"></th>
              </tr>
            </thead>
            <tbody>
              <?php foreach ($PersonData as $item) { ?>
                <tr>
                  <td><?php echo $item['id']; ?></td>
                  <td>
                    <a href="<?php echo $item['avatar']; ?>" target="_blank">
                      <img src="<?php echo is_file('.' . $item['avatar']) ? $item['avatar'] : '/assets/images/avatar.png'; ?>" style="width: 25px;height:25px" alt="">
                    </a>
                  </td>
                  <td><?php echo $item['name']; ?></td>
                  <td><?php echo $item['mobile']; ?></td>
                  <td><?php echo $item['email']; ?></td>
                  <td><?php echo $item['gender'] == 1 ? '男' : ($item['gender'] == 2 ? '女' : '保密'); ?></td>
                  <td><?php echo $item['dname']; ?></td>
                  <td><?php echo $item['jname']; ?></td>
                  <td><?php echo date('Y-m-d H:i', $item['createtime']); ?></td>
                  <td>
                    <a href="PersonEdit.php?id=<?php echo $item['id']; ?>"><i class="icon-pencil"></i></a> <!-- 编辑 -->
                    <a href="#myModal" class="delete" data-id="<?php echo $item['id']; ?>" role="button" data-toggle="modal"><i class="icon-remove"></i></a>
                  </td>
                </tr>
              <?php } ?>
            </tbody>
          </table>
        </div>
        <div class="pagination">
          <?php echo $ShowPage; ?>
        </div>

        <div class="modal small hide fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
          <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
            <h3 id="myModalLabel">删除</h3>
          </div>
          <div class="modal-body">
            <p class="error-text"><i class="icon-warning-sign modal-icon"></i>是否确认删除该员工？</p>
          </div>
          <div class="modal-footer">
            <button class="btn" data-dismiss="modal" aria-hidden="true">取消</button>
            <button class="btn btn-danger" id="delete" data-dismiss="modal">删除</button>
          </div>
        </div>

        <?php include_once('common/footer.php'); ?>
      </div>
    </div>
  </div>

</body>

</html>

<script>
  // 员工id
  let personid = 0;
  // 获取当前点击按钮父级索引
  let index = 0;

  // 列表的删除按钮事件
  $('.delete').click(function() {
    personid = $(this).data('id');

    index = $(this).parents('tr').index();
  })

  $('#delete').click(function() {
    if (!personid) {
      alert('未知员工');

      return false;
    }

    $.ajax({
      type: 'post',
      url: 'api.php',
      data: {
        personid,
        action: 'PersonDel'
      },
      dataType: 'json',
      success: (res) => {
        if (res.code === 1) {
          alert(res.msg);

          // $('.table tbody tr').eq(index).remove();

          // 第二种方法
          location.reload();
        } else {
          alert(res.msg);
        }
      }
    })
  })
</script>